package logger

import (
	"context"
	"ginpro/app/pkg/logger"
	"time"

	"github.com/gin-gonic/gin"
)

func Logger() gin.HandlerFunc {
	return func(c *gin.Context) {
		// 注入请求信息到上下文
		ctx := context.WithValue(c.Request.Context(), "request_info", map[string]interface{}{
			"path":   c.Request.URL.Path,
			"method": c.Request.Method,
			"time":   time.Now(),
		})
		c.Request = c.Request.WithContext(ctx)
		c.Next()
	}
}
func GinLogger() gin.HandlerFunc {
	return func(c *gin.Context) {
		start := time.Now()
		c.Next()

		logger.SugarLogger.Infow("request",
			"status", c.Writer.Status(),
			"method", c.Request.Method,
			"path", c.Request.URL.Path,
			"query", c.Request.URL.RawQuery,
			"ip", c.ClientIP(),
			"latency", time.Since(start),
		)
	}
}
